$(document).ready(function () {
    var page = 0;
    var total = 0;
    $.getJSON("fontend.php?class=userlib&action=history", function (data) {
        $.each(data, function (i, item) {
            $('#tbhistory').append('<tr><th>' + (i + 1) + '</th><td>' + item.book + '</td><td>' + item.action + '</td><td>' + item.time + '</td></tr>');
        });
        total = Object.keys(data).length;
        if (total < 10) {
            $('#histnext').parent().addClass('disabled');
        } else {
            $('#histnext').parent().removeClass('disabled');
        }
    });
    $('#histnext').click(function () {
        page = page + 1;
        $('#histprev').parent().removeClass('disabled');
        $.getJSON("fontend.php?class=userlib&action=history&page=" + page, function (data) {
            total = Object.keys(data).length;
            if (total < 10) {
                $('#histnext').parent().addClass('disabled');
            } else {
                $('#histnext').parent().removeClass('disabled');
            }
            if (total === 0) {
                return;
            } else {
                $('#tbhistory').html('');
            }
            $.each(data, function (i, item) {
                $('#tbhistory').append('<tr><th>' + (i + 1) + '</th><td>' + item.book + '</td><td>' + item.action + '</td><td>' + item.time + '</td></tr>');
            });

        });
    });
    $('#histprev').click(function () {
        if (page > 0) {
            page = page - 1;
            $.getJSON("fontend.php?class=userlib&action=history&page=" + page, function (data) {
                total = Object.keys(data).length;
                if (total < 10) {
                    $('#histnext').parent().addClass('disabled');
                } else {
                    $('#histnext').parent().removeClass('disabled');
                }
                if (total === 0) {
                    return;
                } else {
                    $('#tbhistory').html('');
                }
                $.each(data, function (i, item) {
                    $('#tbhistory').append('<tr><th>' + (i + 1) + '</th><td>' + item.book + '</td><td>' + item.action + '</td><td>' + item.time + '</td></tr>');
                });
            });
        }else{
            $('#histprev').parent().addClass('disabled');
        }
    });
});


